/*
 *  $Id: FinderDialog.java 324 2009-11-16 17:07:40Z martinlaz $
 *
 *  Author:
 *     Martin Lazarov [mlazarov at sfs.uni-tuebingen.de]
 *     
 *  This file is part of the Gralej system
 *     http://code.google.com/p/gralej/
 *
 *  Gralej is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Gralej is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package gralej.blocks.finder;

import gralej.blocks.BlockPanel;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 *
 * @author  Martin
 */
public class FinderDialog extends javax.swing.JDialog {
    
    FinderOptions opts;
    
    /** Creates new form FinderDialog */
    FinderDialog(Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(parent);
        JComponent c = (JComponent) getContentPane();
        c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("ENTER"), "find");
        c.getActionMap().put("find",
            new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    _bFindActionPerformed(e);
                }
        });
        c.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("ESCAPE"), "close");
        c.getActionMap().put("close",
            new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    _bCloseActionPerformed(e);
                }
        });
    }
    
    public static Finder getFinder(Frame parent, BlockPanel panel) {
        FinderDialog dlg = new FinderDialog(parent, true);
        dlg.setVisible(true);
        if (dlg.opts != null)
            return Finder.newInstance(dlg.opts, panel);
        return null;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        _txt = new javax.swing.JTextField();
        _bFind = new javax.swing.JButton();
        _bClose = new javax.swing.JButton();
        _chkRegex = new javax.swing.JCheckBox();
        _chkCaseSensitive = new javax.swing.JCheckBox();
        _chkCompleteMatch = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find");

        jLabel1.setText("Search for:");

        _bFind.setText("Find");
        _bFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _bFindActionPerformed(evt);
            }
        });

        _bClose.setText("Close");
        _bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _bCloseActionPerformed(evt);
            }
        });

        _chkRegex.setMnemonic('r');
        _chkRegex.setText("Regular Expression");

        _chkCaseSensitive.setMnemonic('c');
        _chkCaseSensitive.setText("Case Sensitive");

        _chkCompleteMatch.setMnemonic('m');
        _chkCompleteMatch.setText("Complete match");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_chkCompleteMatch)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(_bFind))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_chkCaseSensitive)
                            .addComponent(_chkRegex))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                        .addComponent(_bClose)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {_bClose, _bFind});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_bFind))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_chkRegex)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_chkCaseSensitive)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_chkCompleteMatch))
                    .addComponent(_bClose))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__bCloseActionPerformed
        dispose();
}//GEN-LAST:event__bCloseActionPerformed

    private void _bFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__bFindActionPerformed
        opts = new FinderOptions();
        opts.text = _txt.getText();
        opts.isRegex = _chkRegex.isSelected();
        opts.isCaseSensitive = _chkCaseSensitive.isSelected();
        opts.isCompleteMatch = _chkCompleteMatch.isSelected();
        dispose();
    }//GEN-LAST:event__bFindActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _bClose;
    private javax.swing.JButton _bFind;
    private javax.swing.JCheckBox _chkCaseSensitive;
    private javax.swing.JCheckBox _chkCompleteMatch;
    private javax.swing.JCheckBox _chkRegex;
    private javax.swing.JTextField _txt;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    
}
